package com.course.db;

import java.sql.ResultSet;

import com.course.db.ExecuteDB;

public class User extends ExecuteDB {

	//members
	private int Id;
	private String name;
	private String password;
	private String type;
	private String strSql;
	
	public User()
	{
		this.Id = 0;
		this.name = "";
		this.password = "";
		this.type = "";
	}
	
	public String CountSql()
	{
		this.strSql = "select count(*) from user";
		return this.strSql;
	} 
	
	//add a user
	public boolean add()
	{

		this.strSql="insert into user ";
		this.strSql=this.strSql + "(name,password,type) ";
		this.strSql=this.strSql + "values('" + this.name + "','" + this.password + "','" + this.type + "')";
		System.out.println(this.strSql);
		boolean isAdd = super.exeSql(this.strSql);
        return isAdd;
	}
	
	public int lastid()
	{
		int lid = 0;
		this.strSql = "select last_insert_id() as id from user limit 1";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
			if (rs.next())
				lid = rs.getInt("id");
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return lid;
	}
	
	//delete a user by id
    public boolean delete(int UserID)
    {
       this.strSql = "delete from user where Id in (";
       this.strSql = this.strSql + UserID +")";

       boolean isDelete = super.exeSql(this.strSql);
       return isDelete;
    }
    
  //initialize user information
 	public boolean init()
	{
		this.strSql="select * from `user` where Id=";
		this.strSql=this.strSql + "'" + this.Id + "'";

		try
		{
			ResultSet rs = super.exeQuery(this.strSql);
			if (rs.next())
			{
				this.Id=rs.getInt("Id");
				this.name=rs.getString("name");
				this.password=rs.getString("password");
				this.type=rs.getString("type");
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
			return false;
		}
	}
 	
 	//modify user infomation
    public boolean modify_user()
	{
		this.strSql="update user set";
		this.strSql=this.strSql + " name=" + "'" + this.name + "',";
		this.strSql=this.strSql + " password=" + "'" + this.password + "',";
		this.strSql=this.strSql + " type=" + this.type;
		this.strSql=this.strSql + " where Id=" + this.Id;
		
		boolean isUpdate = super.exeSql(this.strSql);

		return isUpdate;
	}
 	
 	//show users
    public ResultSet show_users()
	{
		this.strSql="select * from `user`";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return rs;
	}
    
	//check user's validation
 	public boolean valid()
	{
		this.strSql="select * from `user` ";
		this.strSql=this.strSql + " where name='" + this.name + "'";
		this.strSql=this.strSql + "  and password='"+ this.password + "'";
		this.strSql=this.strSql + " and type='" + this.type +"'";
		System.out.println(this.strSql);

		try
		{
			ResultSet rs = super.exeQuery(this.strSql);
			if (rs.next())
			{
				this.Id=rs.getInt("Id");
				//this.username=rs.getString("username");
				//this.usertype = rs.getInt("usertype");
				//init();
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception ex)
		{
			return false;
		}
	}

	public int getId() {
		return Id;
	}

	public void setId(int id) {
		Id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getStrSql() {
		return strSql;
	}

	public void setStrSql(String strSql) {
		this.strSql = strSql;
	}
    
    
}
